<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/20
 * Time: 14:17
 */

namespace App\Rep;


use App\Models\Invoices;
use App\Models\OrderChild;

class NumberExtractorRep
{

    /**
     * 查询可领取订单
     * @param $params
     * @return array
     */
    public function getOrderListByEmployeeCardNumnberRep($params){
        $employeeRep = new EmployeeRep();
        $list = $employeeRep->searchPensionableOrderChildRep($params);
        return ['code'=>0,'data'=>$list,'msg'=>'success'];
    }

    /**
     * 查询外包订单
     * @param $params
     */
    public function getEpibolyOrderListByEmployeeCardNumnberRep($params){
        $list = OrderChild::where('is_epiboly',1)
            ->with(['job_type','employee','order'=>function($q){
                return $q->with(['color'=>function($q){
                    return $q->select('id','color');
                },'product'=>function($q){
                    return $q->select('id','product_name');
                },'customer'=>function($q){
                    return $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    return $q->select('id','model_number');
                },'specification'=>function($q){
                    return $q->select('id','specification');
                }]);
            }])
            ->paginate($params['per_page']??10);
        return $list;
    }

    /**
     * 打印外包订单
     */
    public function getEpibolyOrderInfoRep($params){
        //p($params['ids']);
        $list = OrderChild::whereIn('id',$params['ids'])
            ->with(['job_type','employee','order'=>function($q){
                return $q->with(['color'=>function($q){
                    return $q->select('id','color');
                },'product'=>function($q){
                    return $q->select('id','product_name');
                },'customer'=>function($q){
                    return $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    return $q->select('id','model_number');
                },'specification'=>function($q){
                    return $q->select('id','specification');
                }]);
            }])
            ->get()->toArray();
        return $list;
    }

    /**
     * 获取订单详情
     * @param $params
     *
     */
    public function getOrderChildInfoByEmployeeCardNumnberRep($params){
        $order_child = OrderChild::where('id',$params['id'])
            ->with(['job_type','employee','order'=>function($q){
                return $q->with(['color'=>function($q){
                    return $q->select('id','color');
                },'product'=>function($q){
                    return $q->select('id','product_name');
                },'customer'=>function($q){
                    return $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    return $q->select('id','model_number');
                },'specification'=>function($q){
                    return $q->select('id','specification');
                }]);
            }])
            ->first()->toArray();
        $res = [
            'title'=>'接单单据',
            'qr_code'=>json_encode(["order_id"=>$order_child['order_id'],"order_child_id"=>$order_child['id']]),
            'order_number'=>$order_child['order']['order_number'],
            'customer_name'=>$order_child['order']['customer']['customer_name'],
            'order_date'=>$order_child['order']['order_date'],
            'delivery_date'=>$order_child['order']['delivery_date'],
            'serial_number'=>$order_child['order']['serial_number'],
            'emergency_procedure'=>$order_child['order']['emergency_procedure'],
            'product_name'=>$order_child['order']['product']['product_name'],
            'model_number'=>'【'.$order_child['order']['product']['product_name'].'】'.$order_child['order']['model_number']['model_number'],
            'specification_id'=>$order_child['order']['specification']['specification'],
            'color'=>$order_child['order']['color']['color'],
            'direction'=>$order_child['order']['direction'],
            'remark'=>$order_child['order']['remark'],
            'draw_time'=>$order_child['draw_time'],
            'employee_user'=>$order_child['employee']['employee_name'],
            'employee_user_id'=>$order_child['employee_user_id'],
            'job_type'=>$order_child['job_type']['job_type'],
        ];
        $list = Invoices::where('is_show',1)->orderBy('sort','asc')->get();
        foreach($list as $k=>$v){
            $array[$k]['field'] = $v->field;
            $array[$k]['size'] = $v->size;
            $array[$k]['field_name'] = $v->field_name;
            $array[$k]['field'] = $v->field;
            $array[$k]['value'] = $res[$v->field];
        }


        return $array;
    }


}